(load "C:\\users\\g9k\\.clisprc.lisp")
(load "C:\\users\\g9k\\Desktop\\lisp\\math.lsp")


(defparameter *box* '(((-1 1 1) (-1 -1 1) (-1 -1 -1) (-1 1 -1) (-1 1 1))
					  ((1 1 1) (-1 1 1) (-1 1 -1) (1 1 -1) (1 1 1))
					  ((1 -1 1) (-1 -1 1) (-1 -1 -1) (1 -1 -1) (1 -1 1))
					  ((1 1 1) (-1 1 1) (-1 -1 1) (1 -1 1) (1 1 1))
					  ((1 1 -1) (-1 1 -1) (-1 -1 -1) (1 -1 -1) (1 1 -1))))

					  
(defparameter *forward* '(-1 0 0))
(defparameter *up* '(0 0 1))
(defparameter *left* '(0 -1 0))
(defparameter *position* '(5 0 0))

(defparameter *screen-width* 800)
(defparameter *screen-height* 600)

(defparameter *flyspeed* 0.1)
(defparameter *turnspeed* 0.1)

(defun window-intersection (point)
	(3d-line-plane-intersection *position* *forward* point (l-op '- (l-op '- *position* (nl-op '* 1.5 *forward*)) point)))

(defun relative-window-coordinates (p)
	`(,(l-dot (l-op '- p *position*)  *left*)
	  ,(l-dot (l-op '- p *position*)  *up*)))

(defun relative-point-to-vertice-point (p)
	(sdl:point :x (+ (/ *screen-width* 2) (* (car p) *screen-height*))
			   :y (+ (/ *screen-height* 2) (* (cadr p) *screen-height*))))
	  
(defun model-to-vertice-points (model)
	(mapcar (lambda (n)
				(mapcar (lambda (m)
							(relative-point-to-vertice-point (relative-window-coordinates (window-intersection m))))
					n))
		model))
		
(defun move (modifier direction)
	(setf *position* (l-op modifier *position* (nl-op '* *flyspeed* direction))))	
	
(defun horizontal-turn (deg)
	())

(defun vertical-turn (deg)
	())
	
(defun projection-test ()
	(sdl:with-init (SDL:SDL-INIT-VIDEO)
		(sdl:window *screen-width* *screen-height* :TITLE-CAPTION "3d box test" :double-buffer t)
		(setf (sdl:frame-rate) 60)

		(sdl:with-events ()
			(:quit-event () t)
			(:key-down-event ()
				(when (sdl:key-down-p :sdl-key-escape)
					(sdl:push-quit-event)))
			
			(:idle ()
				(sdl:clear-display sdl:*black*)
				
				(when (sdl:key-down-p :sdl-key-space)
					(move '- *up*))
				(when (sdl:key-down-p :sdl-key-x)
					(move '+ *up*))
				(when (sdl:key-down-p :sdl-key-d)
					(move '+ *left*))
				(when (sdl:key-down-p :sdl-key-a)
					(move '- *left*))
				(when (sdl:key-down-p :sdl-key-w)
					(move '+ *forward*))
				(when (sdl:key-down-p :sdl-key-s)
					(move '- *forward*))
				
				(map 'list (lambda (n) (sdl:draw-filled-polygon n :color sdl:*white*))
								(model-to-vertice-points *box*))
								
				(sdl:update-display)))))

(projection-test)